package com.sun.sviewhelper.demo;

import android.content.Context;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sun.sviewhelper.base.BaseActivity;
import com.sun.sviewhelper.base.BaseAdapter;
import com.sun.sviewhelper.databinding.ActivityListBinding;
import com.sun.sviewhelper.databinding.ItemListBinding;

import java.util.ArrayList;
import java.util.List;

public class ListActivity extends BaseActivity<ActivityListBinding> {
    @Override
    protected void initView(Bundle savedInstanceState) {
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        MyAdapter adapter = new MyAdapter(this);
        mBinding.recyclerView.setAdapter(adapter);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 30; i++)
            list.add(String.valueOf(i));
        adapter.setDataList(list);
        adapter.setOnItemClickListener((view, position) -> {
            adapter.select(position);
        });
    }

    private class MyAdapter extends BaseAdapter<String, ItemListBinding> {
        private int selectIndex = -1;

        public MyAdapter(Context context) {
            super(context);
        }

        @Override
        protected void onBeforeBind(BaseViewHolder<ItemListBinding> holder, int position) {
            holder.getItemBinding().titleView.setText(getDataList().get(position));
            holder.getItemBinding().selectView.setSelected(selectIndex == position);
           /* holder.getItemBinding().selectView.setOnClickListener(v -> {
                select(position);
            });*/
        }

        public void select(int pos) {
            this.selectIndex = pos;
            notifyDataSetChanged();
        }
    }
}
